--
-- Author: shunguo.chen
-- Date: 2016-10-19 17:56:54
--
-- RewardFameRender

local ClassRef = app.mvc.createRenderClass(...)

--data = RewardFameVo
function ClassRef.create(data, node)
	local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.RewardFameNode))
	if node == nil then
		ccui.Helper:doLayout(sprite)
	end
	sprite:init(data)
	return sprite
end       

function ClassRef:init(data)
	self.super.init(self, data)

	self:setContentSize(self._ui.Panel_1:getContentSize())

	self._curActName = "a"
	self._action = self:getTimeline()
	display.uiAddClick(self._ui.a, function()
			self:flipAction()
			executeFunc(self._onBtnClick, "flip")
		end, false)

	self._btn = self._ui.a:findChild("Button")
	self._btn:addClickEventListener(function()
			if self._data.isOpen then
				local daibi = UD:getDaibiValue(self._data.moneyId)
				if daibi >= self._data.moneyCost then     
					executeFunc(self._onBtnClick, "click")
				else
					display.pushToast(L("tips_1020004"))
				end
			else
				display.pushToast(L("tips_1120001"))
			end
		end)

	self:refresh()
end

function ClassRef:refresh()
	if self._data then
		self._ui.map:loadTexture(self._data.icon)

		self._ui.a:findChild("tlt"):setString(self._data.mapName)
		self._ui.b:findChild("tlt"):setString(self._data.mapName)
		self._ui.b:findChild("Text"):setString(L(self._data.desc))

		self._ui.icon:loadTexture(self._data.moneyIcon)
		self._ui.num:setString(self._data.moneyCost)

		if self._data.isComp then
			self._ui.num:setColor(display.COLOR_WHITE)
		else
			self:refreshDaibi()
		end

		self._btn:setVisible(not self._data.isComp)
		-- self._btn:setTouchEnabled(self._data.isOpen)
		-- self._btn:setBright(self._data.isOpen)
		-- display.setBtnTouchEnable(self._btn, self._data.isOpen)

		self._ui.name_btn:setVisible(not self._data.isComp)
		self._ui.name_over:setVisible(self._data.isComp)
	end
end

function ClassRef:refreshDaibi()
	if not self._data.isComp then
		local daibi = UD:getDaibiValue(self._data.moneyId)
		local color = daibi >= self._data.moneyCost and display.COLOR_WHITE or display.COLOR_RED
		self._ui.num:setColor(color)
	end
end

function ClassRef:flipAction()
	self._action:play(self._curActName, false)
	if self._curActName == "a" then
		self._curActName = "b"
	else
		self._curActName = "a"
	end
	self._btn:setVisible(not self._data.isComp and self._curActName == "a")
end

function ClassRef:flipFront()
	if self._curActName == "b" then
		self._action:play(self._curActName, false)
		self._curActName = "a"
		self._btn:setVisible(not self._data.isComp)
	end
end

function ClassRef:onBtnClick(cb)
	self._onBtnClick = cb
end

return ClassRef